<template>
    <el-container>
        <el-header>
            <div class="title">
                <img src="../assets/lovely.jpg" alt="">
                <h2>喵喵喵后台管理系统</h2>
            </div>
            <el-button type="danger" class="btn" @click="logout">退出</el-button>
        </el-header>
        <el-container>
            <!-- 侧边栏 -->
            <el-aside :width="isCollapse ? '64px' : '200px'">
                <div class="toggle-button" @click="toggle">|||</div>
                <el-menu
                    background-color="#545c64"
                    text-color="#fff"
                    active-text-color="#ffd04b" 
                    unique-opened 
                    :collapse='isCollapse' 
                    :collapse-transition='false' 
                    :default-active='activePath'
                    router>
                    <el-submenu :index="item.id + ''" :key='item.id' v-for='item in menuList'>
                        <!-- 一级菜单 -->
                        <template slot="title">
                            <i :class="iconObj[item.id]" class="icon"></i>
                            <span>{{item.authName}}</span>
                        </template>
                        <!-- 二级菜单 -->
                        <el-menu-item :index="'/' + val.path" :key='val.id' v-for='val in item.children' @click="saveNavLink('/' + val.path)"> 
                            <template slot="title">
                                <i class="el-icon-star-on"></i>
                                {{val.authName}}
                            </template>
                        </el-menu-item>
                    </el-submenu>
                </el-menu>
            </el-aside>
            <el-main>
                <!-- 占位符 挖坑 等子路由来填 -->
                <router-view></router-view>
            </el-main>
        </el-container>
    </el-container>
    

</template>

<script>
export default {
    data() {
        return {
            menuList:[],
            iconObj:{
                "125":"el-icon-s-custom",
                "103":"el-icon-s-tools",
                "101":"el-icon-s-goods",
                "102":"el-icon-s-order",
                "145":"el-icon-message-solid"
            },
            isCollapse:false,
            activePath:''
        }
    },
    created() {
        this.getMenuList()
        this.activePath = window.sessionStorage.getItem('path')
    },
    methods: {
        logout() {
            window.sessionStorage.clear()
            this.$router.push('/login')
        },
        async getMenuList() {
            let {data : res} = await this.$http.get('menus')
            // console.log(res)
            if(res.meta.status != 200) return this.$message.error(res.meta.msg)
            this.menuList = res.data
        },
        toggle() {
            // 折叠
            this.isCollapse = !this.isCollapse;
        },
        saveNavLink(path) {
            // 点击高亮
            this.activePath = path
            window.sessionStorage.setItem('path',path)
        }
    }
}
</script>

<style lang="less" scoped>
    .el-container {
        height: 100%;
        .el-header {
            height: 100px;
            background-color: #2b4b6b;
            .title {
                float: left;
                display: flex;
                align-items: center;
                img {
                    width: 50px;
                    height: 50px;
                    border-radius: 50%;
                    border:1px solid pink;
                    margin-top:5px;
                }
                h2 {
                    margin-left: 20px;
                    color: rgb(235, 97, 97);
                }
            }
            .btn {
                float: right;
                margin: 10px;
            }
        }
        .el-aside {
            background-color: rgb(235, 97, 97);
            height: 100%;
            .icon {
                margin:10px 30px 0 0;
            }
            .toggle-button {
                height: 24px;
                line-height: 24px;
                font-size: 10px;
                color: #fff;
                text-align: center;
                background-color: #ffd04b;
                letter-spacing: 0.2em;
                cursor: pointer;
            }
        }
    }
</style>

